# Carnal Realms negotiated-marriage eligibility helpers.

cr_marriage_still_eligible_character_trigger = {
	is_adult = yes
	can_marry = yes
	prisoner = no
	is_incapable = no
	is_inaccessible_trigger = no
}

cr_marriage_available_character_trigger = {
	cr_marriage_still_eligible_character_trigger = yes
	NOT = { has_character_flag = cr_marriage_negotiation_active }
}

# Character scope. CK2 exposes the matrilineal game rule, but it does not expose
# a general trigger for the combined religion/government permission. The known
# vanilla prohibitions are mirrored here while title succession law remains
# free to grant the ordinary engine interaction separately.
cr_matrilineal_terms_permitted_trigger = {
	has_game_rule = {
		name = matrilineal_marriages
		value = on
	}
	NOT = { religion_group = muslim }
	is_merchant_republic = no
	is_patrician = no
}

# Character scope. FROM is the other prospective spouse. This mirrors the
# active cross-group intermarry links in CR's religion definition when the
# interfaith game rule is restricted.
cr_religions_may_intermarry_trigger = {
	OR = {
		religion_group = FROM
		AND = {
			religion = nestorian
			FROM = { OR = { religion = bon religion = buddhist religion = taoist religion_group = zoroastrian_group } }
		}
		AND = {
			FROM = { religion = nestorian }
			OR = { religion = bon religion = buddhist religion = taoist religion_group = zoroastrian_group }
		}
		AND = {
			religion = qarmatian
			FROM = { religion_group = zoroastrian_group }
		}
		AND = {
			FROM = { religion = qarmatian }
			religion_group = zoroastrian_group
		}
		AND = {
			religion = norse_pagan
			FROM = { OR = { religion = buddhist religion_group = zoroastrian_group } }
		}
		AND = {
			FROM = { religion = norse_pagan }
			OR = { religion = buddhist religion_group = zoroastrian_group }
		}
		AND = {
			OR = { religion = zun_pagan religion = bon }
			FROM = { OR = { religion_group = indian_group religion_group = zoroastrian_group } }
		}
		AND = {
			FROM = { OR = { religion = zun_pagan religion = bon } }
			OR = { religion_group = indian_group religion_group = zoroastrian_group }
		}
		AND = {
			religion_group = zoroastrian_group
			FROM = { OR = { religion = buddhist religion = tengri_pagan } }
		}
		AND = {
			FROM = { religion_group = zoroastrian_group }
			OR = { religion = buddhist religion = tengri_pagan }
		}
		AND = {
			religion = manichean
			FROM = { religion = taoist }
		}
		AND = {
			FROM = { religion = manichean }
			religion = taoist
		}
		AND = {
			religion = buddhist
			FROM = { religion = tengri_pagan }
		}
		AND = {
			FROM = { religion = buddhist }
			religion = tengri_pagan
		}
	}
}

# Target scope in cr_negotiate_marriage. FROM is the proposing ruler.
cr_marriage_target_eligible_trigger = {
	cr_marriage_available_character_trigger = yes
	NOT = { character = FROM }
	is_within_diplo_range = FROM
	OR = {
			has_game_rule = {
				name = interfaith_marriages
				value = open
			}
		cr_religions_may_intermarry_trigger = yes
	}
	OR = {
		NOT = { is_close_relative = FROM }
		AND = {
			NOT = { religion_group = jewish_group }
			FROM = { NOT = { religion_group = jewish_group } }
			OR = {
				religion = catholic
				religion = messalian
				religion_group = zoroastrian_group
				FROM = { religion = catholic }
				FROM = { religion = messalian }
				FROM = { religion_group = zoroastrian_group }
			}
		}
	}
}

# The current offer author may submit to the recipient only where CK2 can form
# an ordinary liege-vassal relationship without breaking an active war or the
# nomadic clan structure.
cr_marriage_can_offer_vassalization_trigger = {
	is_landed = yes
	independent = yes
	war = no
	NOT = { tier = EMPEROR }
	NOT = { is_nomadic = yes }
	event_target:cr_marriage_offer_recipient = {
		is_landed = yes
		independent = yes
		war = no
		NOT = { is_nomadic = yes }
		higher_real_tier_than = event_target:cr_marriage_offer_author
	}
}

# Every category is selected before an offer can be sent. All checks are
# repeated on acceptance because rank, war, eligibility, or game rules may have
# changed while the messenger was travelling.
cr_marriage_settlement_valid_trigger = {
	event_target:cr_marriage_proposer = {
		cr_marriage_still_eligible_character_trigger = yes
		has_character_flag = cr_marriage_negotiation_active
	}
	event_target:cr_marriage_candidate = {
		cr_marriage_still_eligible_character_trigger = yes
		has_character_flag = cr_marriage_negotiation_active
	}
	event_target:cr_marriage_offer_author = {
		OR = {
			has_character_flag = cr_marriage_term_patrilineal
			has_character_flag = cr_marriage_term_matrilineal
		}
		OR = {
			has_character_flag = cr_marriage_author_keep_religion
			has_character_flag = cr_marriage_author_personal_conversion
			has_character_flag = cr_marriage_author_realm_conversion
		}
		OR = {
			has_character_flag = cr_marriage_recipient_keep_religion
			has_character_flag = cr_marriage_recipient_personal_conversion
			has_character_flag = cr_marriage_recipient_realm_conversion
		}
		OR = {
			has_character_flag = cr_marriage_author_keep_religion
			AND = {
				OR = {
					has_character_flag = cr_marriage_author_personal_conversion
					has_character_flag = cr_marriage_author_realm_conversion
				}
				has_character_flag = cr_marriage_recipient_keep_religion
			}
		}
		OR = {
			has_character_flag = cr_marriage_author_stays_independent
			has_character_flag = cr_marriage_author_offers_vassalization
		}
	}
	OR = {
		event_target:cr_marriage_offer_author = {
			has_character_flag = cr_marriage_term_patrilineal
		}
		AND = {
			event_target:cr_marriage_offer_author = {
				has_character_flag = cr_marriage_term_matrilineal
				cr_matrilineal_terms_permitted_trigger = yes
			}
			event_target:cr_marriage_offer_recipient = {
				cr_matrilineal_terms_permitted_trigger = yes
			}
		}
	}
	OR = {
		event_target:cr_marriage_offer_author = {
			has_character_flag = cr_marriage_author_stays_independent
		}
		event_target:cr_marriage_offer_author = {
			has_character_flag = cr_marriage_author_offers_vassalization
			cr_marriage_can_offer_vassalization_trigger = yes
		}
	}
}
